﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Ionic.Zip;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Terraria
{
	// Token: 0x0200004E RID: 78
	public class TexturePackSupport
	{
		// Token: 0x060006C9 RID: 1737 RVA: 0x0036AC90 File Offset: 0x00368E90
		public static bool FetchTexture(string path, out Texture2D tex)
		{
			ZipEntry zipEntry;
			if (TexturePackSupport.entries.TryGetValue(path, out zipEntry))
			{
				using (MemoryStream memoryStream = new MemoryStream())
				{
					zipEntry.Extract(memoryStream);
					tex = TexturePackSupport.FromStreamSlow(Main.instance.GraphicsDevice, memoryStream);
					TexturePackSupport.ReplacedTextures++;
					return true;
				}
			}
			tex = null;
			return false;
		}

		// Token: 0x060006CA RID: 1738 RVA: 0x0036ACFC File Offset: 0x00368EFC
		public static Texture2D FromStreamSlow(GraphicsDevice graphicsDevice, Stream stream)
		{
			Texture2D texture2D = Texture2D.FromStream(graphicsDevice, stream);
			Color[] array = new Color[texture2D.Width * texture2D.Height];
			texture2D.GetData<Color>(array);
			for (int num = 0; num != array.Length; num++)
			{
				array[num] = Color.FromNonPremultiplied(array[num].ToVector4());
			}
			texture2D.SetData<Color>(array);
			return texture2D;
		}

		// Token: 0x060006CB RID: 1739 RVA: 0x0036AD5C File Offset: 0x00368F5C
		public static void FindTexturePacks()
		{
			string path = Path.Combine(Main.SavePath, "TexturePacks");
			if (TexturePackSupport.Enabled)
			{
				Directory.CreateDirectory(path);
			}
			TexturePackSupport.entries.Clear();
			TexturePackSupport.texturePacks.Clear();
			if (Directory.Exists(path))
			{
				List<string> list = Directory.GetFiles(path).ToList<string>();
				list.Sort();
				foreach (string path2 in list)
				{
					ZipFile zipFile = ZipFile.Read(File.OpenRead(path2));
					TexturePackSupport.texturePacks.Add(zipFile);
					foreach (ZipEntry zipEntry in zipFile.Entries)
					{
						TexturePackSupport.entries.Add(zipEntry.FileName.Replace("/", "\\"), zipEntry);
					}
				}
			}
		}

		// Token: 0x04000CDE RID: 3294
		public static bool Enabled = false;

		// Token: 0x04000CDF RID: 3295
		public static int ReplacedTextures = 0;

		// Token: 0x04000CE0 RID: 3296
		private static List<ZipFile> texturePacks = new List<ZipFile>();

		// Token: 0x04000CE1 RID: 3297
		private static Dictionary<string, ZipEntry> entries = new Dictionary<string, ZipEntry>();

		// Token: 0x04000CE2 RID: 3298
		private static Stopwatch test = new Stopwatch();
	}
}
